//获取参数
var dataType = getQueryString('type');
var num = 1;
var app =angular.module("myApp",[]);

app.controller("myCtrl",function($scope,$http,getImformation){
	switch (dataType){
		case "NEWS_INFO" : 
		    $scope.title = "新闻信息";
		    break;
		case "INDUSTRY_DYNAMICS" : 
		    $scope.title = "行业动态";
		    break;
		case "POLICY_ANNOUNCEMENT" : 
		    $scope.title = "政策公告";
		    break;
		case "PLANTING_TECHIQUE" : 
		    $scope.title = "种植技术";
		    break;    
	}
	getImformation.initImformation(dataType,1,function(response){
		$scope.arr = response.data.list;
		console.log(response);
	});
	
	//angular的监听事件
    
    window.addEventListener("scroll", function(event) {
    	var clientHeight = $(this).height();
    	var scrollTop = $(this).scrollTop();
    	var docHeight = $(document).height();
    	//判断是否到达底部，上拉加载
        if(clientHeight + scrollTop >= docHeight){
        	num++;
        	getImformation.initImformation(dataType,num,function(response){
        		if(num > response.data.pager.pageCount){
					return;
				}
				$scope.arr = $scope.arr.concat(response.data.list);				
			})
	    }    
    });
})

//创建ng的工厂,请求商品的数据，进行依赖注入
app.factory("getImformation",function($http){
	var obj = {};
	obj.initImformation = function(x,y,_callback){
		var params = {
			infoType: x,  
		    pageNum : y,
		    pageSize : 10
		};
		params = $.param(params);
		$http({
	        method : "post",
	        dataType: "json",
			withCredentials: true,	//跨域名请求。XHR：XMLHttpRequest (XHR) ，基于XML技术的Http请求
		    crossDomain: true,
		    headers:{'Content-Type': 'application/x-www-form-urlencoded'},
		    data: params,
	        async:true,
	        url : host + "/terminal/info/infoTypeArticle"
	   }).success(function(response){
	        if(response.code == 2000){
	        	_callback(response);
	        }
	    }).error(function(response){
	    	console.log(response);
	    });
	}
	return obj;
})